from django.db import models
from django.conf import settings
from django.contrib.auth import get_user_model

User = get_user_model()

class Patient(models.Model):
    """患者模型"""
    GENDER_CHOICES = [
        ('M', '男'),
        ('F', '女'),
    ]
    STATUS_CHOICES = [
        ('waiting', '待诊'),
        ('called', '已叫号'),
        ('diagnosing', '诊断中'),
        ('completed', '已完成')
    ]

    name = models.CharField(max_length=100, verbose_name='姓名', default='未知')
    gender = models.CharField(max_length=10, choices=[('M', '男'), ('F', '女')], default='M', verbose_name='性别')
    age = models.IntegerField(default=0, verbose_name='年龄')
    phone = models.CharField(max_length=11, blank=True, null=True, verbose_name='联系电话')
    address = models.TextField(blank=True, null=True, verbose_name='地址')
    id_number = models.CharField(max_length=18, unique=True, verbose_name='身份证号', blank=True, null=True)
    medical_record_no = models.CharField(max_length=20, unique=True, verbose_name='病历号', blank=True, null=True)
    medical_history = models.TextField(blank=True, null=True, verbose_name='病史')
    allergies = models.TextField(blank=True, null=True, verbose_name='过敏史')
    registration_time = models.DateTimeField(verbose_name='挂号时间', blank=True, null=True)
    doctor = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='主治医生')
    status = models.CharField(max_length=20, choices=[
        ('waiting', '待诊'),
        ('called', '已叫号'),
        ('diagnosing', '诊断中'),
        ('completed', '已完成')
    ], default='waiting', verbose_name='状态')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '患者'
        verbose_name_plural = '患者'
        ordering = ['-registration_time']

    def __str__(self):
        return f"{self.name} ({self.get_gender_display()}, {self.age}岁)"


class Diagnosis(models.Model):
    """诊断记录模型"""
    patient = models.ForeignKey(Patient, on_delete=models.CASCADE, related_name='diagnoses', verbose_name='患者')
    doctor = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='diagnoses', verbose_name='医生', null=True, blank=True)
    chief_complaint = models.TextField(verbose_name='主诉', default='无')
    present_illness = models.TextField(verbose_name='现病史', default='无')
    past_history = models.TextField(verbose_name='既往史', default='无')
    allergy_history = models.TextField(verbose_name='过敏史', blank=True, null=True)
    physical_examination = models.TextField(verbose_name='体格检查', default='无特殊')
    auxiliary_examination = models.TextField(verbose_name='辅助检查', blank=True, null=True)
    diagnosis = models.TextField(verbose_name='诊断', default='待诊断')
    treatment_plan = models.TextField(verbose_name='治疗方案', default='待制定')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '诊断记录'
        verbose_name_plural = '诊断记录'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.patient.name} - {self.diagnosis} - {self.created_at.strftime('%Y-%m-%d')}"
